﻿// fct_uart_tool.cpp: 实现文件
//

#include "pch.h"
#include "MFCApplication1.h"
#include "afxdialogex.h"
#include "fct_uart_tool.h"
#include "logout.h"
#include "SelfTime.h"
#include "commom.h"

static char BaudRateList[][16] = { "1200", "2400", "4800", "9600", "115200",
"128000", "230400", "256000", "460800", "921600" };

// fct_uart_tool 对话框
fct_uart_tool* pfct_uart_tool = NULL;
IMPLEMENT_DYNAMIC(fct_uart_tool, CDialogEx)

fct_uart_tool::fct_uart_tool(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_UART_TOOL, pParent)
{
	pfct_uart_tool = this;
}

fct_uart_tool::~fct_uart_tool()
{
	pfct_uart_tool = NULL;
	UartisOpen = false;
	USER_PRINTF_LOG(LOG_FILE, "fct_uart_tool exit");
}

void fct_uart_tool::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMBO_SET_BAND, m_fctUartBaudRate);
	DDX_Control(pDX, IDC_COMBO_SET_UART, m_fctUartPort);
}


BEGIN_MESSAGE_MAP(fct_uart_tool, CDialogEx)
	ON_BN_CLICKED(IDC_OPEN_UART, &fct_uart_tool::OnBnClickedOpenUart)
	ON_BN_CLICKED(IDC_UART_SEND_BTN, &fct_uart_tool::OnBnClickedUartSendBtn)
	ON_CBN_SELCHANGE(IDC_COMBO_SET_UART, &fct_uart_tool::OnCbnSelchangeComboSetUart)
	ON_CBN_SELCHANGE(IDC_COMBO_SET_BAND, &fct_uart_tool::OnCbnSelchangeComboSetBand)
	ON_BN_CLICKED(IDC_UART_CLEAR_RECV, &fct_uart_tool::OnBnClickedUartClearRecv)
END_MESSAGE_MAP()

BOOL fct_uart_tool::OnInitDialog()
{
	CDialogEx::OnInitDialog();


	static HWND hWndEdit = GetDlgItem(IDC_WRITE_UART_SEND)->GetSafeHwnd();
	// 调用 DisableIME 函数禁用中文输入法
	DisableIME(hWndEdit);

	SerialPortBaudrateCtrlInit();
	SerialPortNumberCtrlInit();

	return TRUE;  // 返回 TRUE，除非将焦点设置到控件
}

//重写虚函数PreTranslateMessage() 用于回车键确认
BOOL fct_uart_tool::PreTranslateMessage(MSG* pMsg)
{
	// TODO:  在此添加专用代码和/或调用基类
	if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_RETURN)
	{
		if (GetFocus()->GetDlgCtrlID() == IDC_WRITE_UART_SEND)//按下回车，如果当前焦点是在自己期望的控件上
		{
			// 你想做的事，如果按下回车时焦点在你想要的控件上
			OnBnClickedUartSendBtn();
		}
		return TRUE;
	}
	if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_ESCAPE)
		return TRUE;
	return CDialogEx::PreTranslateMessage(pMsg);
}

//初始化波特率控件
void fct_uart_tool::SerialPortBaudrateCtrlInit(void)
{
	int total = sizeof(BaudRateList) / sizeof(BaudRateList[0]);
	m_fctUartBaudRate.ResetContent();

	for (int index = 0; index < total; index++) {
		m_fctUartBaudRate.AddString((LPCTSTR)BaudRateList[index]);
	}

	m_fctUartBaudRate.SetCurSel(4);
	return;
}

//初始化串口号控件
void fct_uart_tool::SerialPortNumberCtrlInit(void)
{
	int portnum = -1;

	UpdateData(true);

	portnum = UpdateSerailPortList();
	USER_PRINTF_LOG(LOG_FILE, "ComPortList : %d", portnum);

	UpdateData(false);

	return;
}

//更新串口号列表
int fct_uart_tool::UpdateSerailPortList(void)
{
	HKEY hKey;
	LONG nRetVal;
	int index = 0;
	char portName[256];
	char commName[256];
	DWORD dwLong, dwSize;

	nRetVal = RegOpenKeyEx(HKEY_LOCAL_MACHINE, HKEY_LOCAL_MACHINE_SERIALCOMM, NULL, KEY_READ, &hKey);
	if (nRetVal != ERROR_SUCCESS) {
		USER_PRINTF_LOG(LOG_FILE, "RegOpenKeyEx error.");
		return -1;
	}

	m_fctUartPort.ResetContent();

	while (1) {
		dwLong = sizeof(portName);
		dwSize = sizeof(portName);
		nRetVal = RegEnumValue(hKey, index, portName, &dwLong, NULL, NULL, (PUCHAR)commName, &dwSize);
		if (nRetVal == ERROR_NO_MORE_ITEMS) {
			break;
		}

		CString strCommName;
		strCommName.Format("%s", commName);
		strCommName += _T("(可用)");

		USER_PRINTF_LOG(LOG_FILE, "COM -> %s", commName);

		m_fctUartPort.AddString((LPCTSTR)commName);

		index++;
	}

	RegCloseKey(hKey);

	return index;
}

void fct_uart_tool::veiwPortRECV(std::string recv) {

	SelfTimeTools time;
	CString temp(recv.c_str());
	//temp = "[" + CString(time.GetSelfCurrentTime().c_str()) + "]"  + "-------<收"+ "\r\n" + temp;
	m_strViewPortRecv =  m_strViewPortRecv + temp;
	
	GetDlgItem(IDC_READ_UART_RECV)->SetWindowText(m_strViewPortRecv);

	CEdit *pViewPortRecv = (CEdit*)GetDlgItem(IDC_READ_UART_RECV);
	pViewPortRecv->LineScroll(pViewPortRecv->GetLineCount());
	int nLength = pViewPortRecv->GetWindowTextLength();
	pViewPortRecv->SetSel(nLength, nLength, SCF_SELECTION);

	temp = _T("");
	temp.Empty();
}

// fct_uart_tool 消息处理程序
static void portRecvCallbackWithParam(int portIndex, const char* data, int length, const std::string& additionalParam) {
	pfct_uart_tool->veiwPortRECV(std::string(data, length));
	USER_PRINTF_LOG("test.log", data);

}


void fct_uart_tool::SetUartBtnStatus()
{
	GetDlgItem(IDC_OPEN_UART)->SetWindowText("打开串口");
}

void fct_uart_tool::OnBnClickedOpenUart()
{


}

void fct_uart_tool::OnBnClickedUartSendBtn()
{
	AfxMessageBox(_T("123456"));
}

void fct_uart_tool::OnBnClickedUartClearRecv()
{
	// TODO: 在此添加控件通知处理程序代码
	m_strViewPortRecv = _T("");

	GetDlgItem(IDC_READ_UART_RECV)->SetWindowText(m_strViewPortRecv);

	CEdit* pViewPortRecv = (CEdit*)GetDlgItem(IDC_READ_UART_RECV);
	pViewPortRecv->LineScroll(pViewPortRecv->GetLineCount());
	int nLength = pViewPortRecv->GetWindowTextLength();
	pViewPortRecv->SetSel(nLength, nLength, SCF_SELECTION);

}

void fct_uart_tool::OnCbnSelchangeComboSetUart()
{
	// TODO: 在此添加控件通知处理程序代码
}

void fct_uart_tool::OnCbnSelchangeComboSetBand()
{
	// TODO: 在此添加控件通知处理程序代码
}

